package com.ruoyi.tzzl.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.tzzl.domain.WzComment;
import com.ruoyi.tzzl.domain.WzDynamic;
import com.ruoyi.tzzl.domain.WzMessageLogging;
import com.ruoyi.tzzl.domain.WzProgramUser;
import com.ruoyi.tzzl.mapper.WzCommentsMapper;
import com.ruoyi.tzzl.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.NumberUp;
import java.awt.font.TextHitInfo;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 动态评论Service业务层处理
 * @author by caoyong
 * @date 2022/11/18
 **/
@Service
public class WzCommentsServiceImpl extends ServiceImpl<WzCommentsMapper, WzComment> implements IWzCommentsService {

    @Autowired
    private IWzProgramUserService userService;
    @Autowired
    private IWzCertificationService wzCertificationService;
    @Autowired
    private IWzMessageLoggingService wzMessageLoggingService;
    @Autowired
    private IWzDynamicService wzDynamicService;
    @Autowired
    private WzCommentsMapper wzCommentsMapper;

    /**
     * 添加评论
     *
     * @param wzComment
     */
    @Override
    public boolean addComment(WzComment wzComment) {
        wzComment.setCreateTime(new Date());
        //添加消息记录
        addMessageRecord(wzComment);
        return  this.save(wzComment);
    }

    /**
     * 回复评论
     *
     * @param wzComment
     */
    @Override
    public boolean replyToComments(WzComment wzComment) {
        wzComment.setCreateTime(new Date());
        //添加消息记录
        addMessageRecord(wzComment);
        return this.save(wzComment);
    }

    /**
     * 查询动态评论列表
     *
     * @param id
     */
    @Override
    public List<WzComment> getByDid(Long id) {
        LambdaQueryWrapper<WzComment> lqw =new LambdaQueryWrapper<>();
        lqw.eq(WzComment::getDid,id);
        lqw.isNull(WzComment::getRid);
        List<WzComment> list =this.list(lqw);
        if (ObjectUtils.isNotEmpty(list)){
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setUser(userService.userDetails(list.get(i).getCid()));
                list.get(i).setAuthentication(wzCertificationService.getIfAuthentication(list.get(i).getWid()));
                list.get(i).setReplyList(this.getByPid(list.get(i).getId()));
            }
        }
        return list;
    }

    /**
     * 查看回复评论集合
     *
     * @param id
     */
    @Override
    public List<WzComment> getByPid(Long id) {
        LambdaQueryWrapper<WzComment> lqw =new LambdaQueryWrapper<>();
        lqw.eq(WzComment::getPid,id);
       List<WzComment> list = this.list(lqw);
       if (ObjectUtils.isNotEmpty(list)){
           for (int i = 0; i < list.size(); i++) {
               list.get(i).setUser(userService.getById(list.get(i).getRid()));
           }
       }
        return list;
    }


    /**
     *  添加消息记录
     * */
    private void addMessageRecord(WzComment wzComment) {
        WzMessageLogging wzMessageLogging =new WzMessageLogging();
        wzMessageLogging.setCreateTime(new Date());
        wzMessageLogging.setProduceType("1");
        //查看生产消息用户信息
        WzProgramUser wzProgramUser = userService.getById(wzComment.getCid());
        wzMessageLogging.setProduceAvatarurl(wzProgramUser.getAvatarurl()); //产生消息的用户头像
        wzMessageLogging.setProduceId(wzProgramUser.getId()); //产生消息的用户id
        wzMessageLogging.setProduceName(wzProgramUser.getUserName()); //产生消息的用户名称
        wzMessageLogging.setMessageContent(wzComment.getComments()); //消息内容
        //查看这个动态是否待有图片
        WzDynamic wzDynamic = wzDynamicService.getById(wzComment.getDid());
        wzMessageLogging.setDynamicId(wzDynamic.getId()); //动态列表id
        if (ObjectUtils.isNotEmpty(wzDynamic.getPictureUrl())){
            wzMessageLogging.setDynamicContent(wzDynamic.getPictureUrl()); //动态消息内容
        }else{
            wzMessageLogging.setDynamicContent(wzDynamic.getThetext()); //动态消息内容
        }
        //查看接收消息用户信息
        WzProgramUser user =userService.getById(wzComment.getWid());
        wzMessageLogging.setReceiceAvatarurl(user.getAvatarurl()); //接收消息的用户头像
        wzMessageLogging.setReceiveId(user.getId()); //接收消息的用户id
        wzMessageLogging.setReceiveName(user.getUserName()); //接收消息的用户名称
        //添加消息记录
        wzMessageLoggingService.save(wzMessageLogging);
    }

}
